
$(document).ready(function(){
	csscody.initialize();
});

jQuery.fsbind = function(object, method){
	var args = Array.prototype.slice.call(arguments, 2);  
	return function() {
		var args2 = [this].concat(args, $.makeArray( arguments ));  
		return method.apply(object, args2);  
	};  
};  

jQuery.fn.fsdelay = function(time,func){
	return this.each(function(){
		setTimeout(func,time);
	});
};


jQuery.fn.extend({
	$chain : [],
	chain: function(fn) {
		this.$chain.push(fn);
		return this;
	},
	callChain: function(context) {
		return (this.$chain.length) ? this.$chain.pop().apply(context, arguments) : false;
	},
	clearChain: function(){
		this.$chain.empty();
		return this;
	}
});

(function($) {

	csscody = {
		getOptions: function() {
			return {
				name            : 'alert',
				zIndex          : 65555,
				onReturn        : false,
				onReturnFunction: function(e) {},
				BoxStyles       : { 'width': 500 },
				OverlayStyles   : { 'backgroundColor': '#000', 'opacity': 0.7 },
				showDuration    : 200,
				closeDuration   : 100,
				moveDuration    : 500,
				onCloseComplete : $.fsbind(this, function() {
					this.options.onReturnFunction(this.options.onReturn);
				})
			};
		},


		initialize: function(options) {
			this.i=0;
			this.options = $.extend(this.getOptions(), options);
			$('body').append('<div id="BoxOverlay"></div><div id="'+this.options.name+'-Box"><div id="'+this.options.name+'-InBox"><div id="'+this.options.name+'-BoxContent"><div id="'+this.options.name+'-BoxContenedor"></div></div></div></div>');

			this.Content    = $('#'+this.options.name+'-BoxContenedor');
			this.Contenedor = $('#'+this.options.name+'-BoxContent');
			this.InBox      = $('#'+this.options.name+'-InBox');
			this.Box        = $('#'+this.options.name+'-Box');

			$('#BoxOverlay').css({
				position        : 'absolute',
				top             : 0,
				left            : 0,
				opacity         : this.options.OverlayStyles.opacity,
				backgroundColor : this.options.OverlayStyles.backgroundColor,
				'z-index'       : this.options.zIndex,
				height          : $(document).height(),
				width           : $(document).width()
			}).hide();

			this.Box.css({
				display         : 'none',
				position        : 'absolute',
				top             : 0,
				left            : 0,
				'z-index'       : this.options.zIndex + 2,
				width           : this.options.BoxStyles.width + 'px'
			});

			this.preloadImages();

			$(window).bind('resize', $.fsbind(this, function(){
				if(this.options.display == 1) {
					$('#BoxOverlay').css({
						height          : 0,
						width           : 0
					});
					$('#BoxOverlay').css({
						height          : $(document).height(),
						width           : $(document).width()
					});
					this.replaceBox();
				}
			}));

			this.Box.bind('keydown', $.fsbind(this, function(obj, event){
				if (event.keyCode == 27){
					this.options.onReturn = false;
					this.display(0);
				}      
			}));

			$(window).bind('scroll', $.fsbind(this, function(){
				this.replaceBox();
			}));

		},

		replaceBox: function() {
			if(this.options.display == 1) {

				this.Box.stop();

				this.Box.animate({
					left  : ( ($(document).width() - this.options.BoxStyles.width) / 2),
					top   : ( $(document).scrollTop() + ($(window).height() - this.Box.outerHeight()) / 2 )
				}, {
					duration  : this.options.moveDuration,
					easing    : 'easeOutBack'
				});

				$(this).fsdelay(this.options.moveDuration, $.fsbind(this, function() {
					$('#BoxAlertBtnOk').focus();
					$('#BoxPromptInput').focus();
					$('#BoxConfirmBtnOk').focus();
				}));
			}
		},

		display: function(option) {
			if(this.options.display == 0 && option != 0 || option == 1) {

				if (!$.support.maxHeight) { //IE6
					$('embed, object, select').css({ 'visibility' : 'hidden' });
				}
				this.togFlashObjects('hidden');
				this.options.display = 1;

				$('#BoxOverlay').stop();
				$('#BoxOverlay').fadeIn(this.options.showDuration, $.fsbind(this, function(){
					this.Box.css({
						display         : 'block',
						left            : ( ($(document).width() - this.options.BoxStyles.width) / 2)
					});
					this.replaceBox();
				}));

			} else {

				this.Box.css({
					display         : 'none',
					top             : 0
				});

				this.options.display = 0;

				$(this).fsdelay(500, $.fsbind(this, this.queue));

				$(this.Content).empty();
				this.Content.removeClass();

				if(this.i==1) {
					$('#BoxOverlay').stop();
					$('#BoxOverlay').fadeOut(this.options.closeDuration, $.fsbind(this, function(){
						$('#BoxOverlay').hide();
						if (!$.support.maxHeight) { //IE6
							$('embed, object, select').css({ 'visibility' : 'hidden' });
						}

						this.togFlashObjects('visible');

						this.options.onCloseComplete.call();
					}));
				}
			}
		},

		messageBox: function(type, message, properties, input) {

			$(this).chain(function () {

				properties = $.extend({
					'textBoxBtnOk'        : 'OK',
					'textBoxBtnCancel'    : 'Cancel',
					'textBoxInputPrompt'  : null,
					'password'            : false,
					'onComplete'          : function(e) {}
				}, properties || {});

				this.options.onReturnFunction = properties.onComplete;

				this.Content.append('<div id="'+this.options.name+'-Buttons"></div>');
				
				if(type == 'alert' || type == 'info' || type == 'error'){
					$('#'+this.options.name+'-Buttons').append('<input id="BoxAlertBtnOk" type="submit" />');

					$('#BoxAlertBtnOk').val(properties.textBoxBtnOk).css({'width':70});

					$('#BoxAlertBtnOk').bind('click', $.fsbind(this, function(){
						this.options.onReturn = true;
						this.display(0);
					}));

					if(type == 'alert') {
						clase = 'BoxAlert';
					} else if(type == 'error') {
						clase = 'BoxError';
					} else if(type == 'info') {
						clase = 'BoxInfo';
					}

					this.Content.addClass(clase).prepend(message);
					this.display(1);

				}else if(type == 'confirm'){

					$('#'+this.options.name+'-Buttons').append('<input id="BoxConfirmBtnOk" type="submit" /> <input id="BoxConfirmBtnCancel" type="submit" />');
					$('#BoxConfirmBtnOk').val(properties.textBoxBtnOk).css({'width':70});
					$('#BoxConfirmBtnCancel').val(properties.textBoxBtnCancel).css({'width':70});

					$('#BoxConfirmBtnOk').bind('click', $.fsbind(this, function(){
						this.options.onReturn = true;
						this.display(0);
					}));

					$('#BoxConfirmBtnCancel').bind('click', $.fsbind(this, function(){
						this.options.onReturn = false;
						this.display(0);
					}));

					this.Content.addClass('BoxConfirm').prepend(message);
					this.display(1);
				}
				else if(type == 'prompt')
					{

					$('#'+this.options.name+'-Buttons').append('<input id="BoxPromptBtnOk" type="submit" /> <input id="BoxPromptBtnCancel" type="submit" />');
					$('#BoxPromptBtnOk').val(properties.textBoxBtnOk).css({'width':70});
					$('#BoxPromptBtnCancel').val(properties.textBoxBtnCancel).css({'width':70});

					type = properties.password ? 'password' : 'text';

					this.Content.prepend('<input id="BoxPromptInput" type="'+type+'" />');
					$('#BoxPromptInput').val(properties.input);
					$('#BoxPromptInput').css({'width':250});

					$('#BoxPromptBtnOk').bind('click', $.fsbind(this, function(){
						this.options.onReturn = $('#BoxPromptInput').val();
						this.display(0);
					}));

					$('#BoxPromptBtnCancel').bind('click', $.fsbind(this, function(){
						this.options.onReturn = false;
						this.display(0);
					}));

					this.Content.addClass('BoxPrompt').prepend(message + '<br />');
					this.display(1);
				}
				else
					{
					this.options.onReturn = false;
					this.display(0);		
				}

			});

			this.i++;

			if(this.i==1) {
				$(this).callChain(this);
			}
		},

		queue: function() {
			this.i--;
			$(this).callChain(this);
		},

		chk: function (obj) {
			return !!(obj || obj === 0);
		},

		togFlashObjects: function(state) {
			var hideobj=new Array("embed", "iframe", "object");
			for (y = 0; y < hideobj.length; y++) {
				var objs = document.getElementsByTagName(hideobj[y]);
				for(i = 0; i < objs.length; i++) {
					objs[i].style.visibility = state;
				}
			}
		},

		preloadImages: function() {
			var img = new Array(2);
			img[0] = new Image();img[1] = new Image();img[2] = new Image();
			img[0].src = this.Box.css('background-image').replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
			img[1].src = this.InBox.css('background-image').replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
			img[2].src = this.Contenedor.css('background-image').replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
		},


		/*
		Property: alert
		Shortcut for alert // http://www.csscody.com/demo   --  more examples here ----//

		Argument:
		properties - see Options in messageBox
		*/		
		alert: function(message, properties) {
			this.messageBox('alert', message, properties);
		},

		/*
		Property: info
		Shortcut for alert info

		Argument:
		properties - see Options in messageBox
		*/		
		info: function(message, properties){
			this.messageBox('info', message, properties);
		},

		/*
		Property: error
		Shortcut for alert error

		Argument:
		properties - see Options in messageBox
		*/		
		error: function(message, properties){
			this.messageBox('error', message, properties);
		},

		/*
		Property: confirm
		Shortcut for confirm

		Argument:
		properties - see Options in messageBox
		*/
		confirm: function(message, properties){
			this.messageBox('confirm', message, properties);
		},

		/*
		Property: prompt
		Shortcut for prompt

		Argument:
		properties - see Options in messageBox
		*/	
		prompt: function(message, input, properties){
			this.messageBox('prompt', message, properties, input);
		}

	};

})(jQuery);